package easyzhou.com.service.impl;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

import easyzhou.com.bean.Result;
import easyzhou.com.bean.SVNChangedLog;
import easyzhou.com.config.InitConfig;
import easyzhou.com.service.IncermentalPageBuilder;
import easyzhou.com.utils.IOUtil;
import easyzhou.com.utils.svn.SVNLogHandler;

public class DefalutIncermentalPageBuilderImpl implements IncermentalPageBuilder {
	private final Result result = new Result();
	private final File localProject;
	private final File outputProjec;

	/**
	 *
	 *
	 * @param localProjectPath
	 *            项目路径
	 * @param outputProjecPath
	 *            输出路径
	 * @author zhoucong
	 * @date 2016年3月28日 上午11:28:20
	 */
	public DefalutIncermentalPageBuilderImpl(String localProjectPath, String outputProjecPath) {
		super();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HHmmssSSS");
		this.localProject = new File(localProjectPath);
		this.outputProjec = new File(outputProjecPath, dateFormat.format(new Date()));
	}

	@Override
	public Result builde(String centerTextAreaText) {
		if (localProject.isDirectory()) {
			result.setStatus(1);
			result.setMsg("文件不存在");
		}
		if (!outputProjec.exists()) {
			outputProjec.mkdirs();
		}
		SVNChangedLog changedLog = new DefaultSVNLogParseServiceImpl().parse(centerTextAreaText);
		Result result = copyFiles(changedLog);
		return result;
	}

	private Result copyFiles(SVNChangedLog changedLog) {
		Set<String> copyingFiles = new HashSet<>();
		copyingFiles.addAll(changedLog.getAddedSet());
		copyingFiles.addAll(changedLog.getModifiedSet());
		for (String filePath : copyingFiles) {
			// 排除配置文件
			if (isExclusionFile(filePath)) {
				continue;
			}

			File files[] = SVNLogHandler.doParsePath(this.outputProjec, this.localProject, filePath);
			boolean isok = doCopyFile(files[0], files[1]);
			if (isok) {
				result.getSuccessfyllyList().add(files);
			} else {
				result.getErrList().add(files);
			}
		}
		StringBuilder sb = new StringBuilder();

		for (String file : changedLog.getDeleteSet()) {
			sb.append("rm -f " + SVNLogHandler.formatOutputFileName(file));
			sb.append("\n");
		}

		result.setDeleteFileList(new LinkedList<>(changedLog.getDeleteSet()));
		return result;
	}

	private boolean isExclusionFile(String filePath) {
		for (String file : InitConfig.getEXCLUSIONS_SOURCES_FILES()) {
			if (filePath.contains(file)) {
				return true;
			}
		}
		return false;
	}

	private boolean doCopyFile(File file, File newFile) {
		if (file.isDirectory()) {
			if (!newFile.exists()) {
				return newFile.mkdirs();
			}
			return true;
		}
		if (!newFile.getParentFile().exists()) {
			if (!newFile.getParentFile().mkdirs()) {
				return false;
			}
		}
		try {
			if (file.exists()) {
				IOUtil.copy(file, newFile);
				return true;
			} else {
				return false;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

}
